<?php
namespace app\wxapp\controller;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Vod\Vod;
use app\common\entity\Result;
use app\common\service\WxPayService;
use app\common\util\Random;
use app\common\wxpay\lib\WxPayApi;
use app\common\wxpay\lib\WxPayDataBase;
use app\common\wxpay\lib\WxPayUnifiedOrder;
use app\common\wxpay\WxPayConfig;
use app\common\xunfei\XFTrans;
use app\Request;
use OSS\Core\OssException;
use OSS\OssClient;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/4 0004
 * Time: 19:51
 */
class Ceshi {
    // 存储空间名称
    private $bucket ="medical-course";

    // Endpoint以杭州为例，其它Region请按实际情况填写。
    //$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    private $endpoint = "http://oss-cn-shenzhen.aliyuncs.com";

    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
    private $accessKeyId = "";
    private $accessKeySecret = "";

    private $ossClient;

    public function __construct()
    {
        $this->accessKeyId = env('aliyun.ali_access_key_id');
        $this->accessKeySecret = env('aliyun.ali_access_key_secret');
        $this->ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
    }

    public function callback(Request $request){
        $param = $request->param();
        $data = input("php://input");
        var_dump($data,$param);
        file_put_contents("tmp/a.txt",serialize($data).serialize($param));
    }

    public function ceshi2(Request $request){
        $word = $request->param("w");
        //$res = file_get_contents("https://fanyi.baidu.com/?aldtype=16047#en/zh/hello");
        $xunfei = new XFTrans();
        $res = $xunfei->xfyun($word);
        //$res = '{"code":0,"data":{"result":{"from":"en","to":"cn","trans_result":{"dst":"你好","src":"hello"}}},"message":"success","sid":"its0008a0b7@dx1754a373537a11d902"}';
        $res = json_decode($res,true);
        if($res['message'] == 'success'){
            echo $res['data']['result']['trans_result']['dst'];
        }else{
            var_dump($res);
        }
    }

    public function ceshi(){
        echo date('Y-m-d H:i:s',1604064629);

    }

    /**
     * 格式化参数格式化成url参数
     */
    public function ToUrlParams($data)
    {
        $buff = "";
        foreach ($data as $k => $v)
        {
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");
        return $buff;
    }

    public function notify(){

    }

    public function createUploadVideo(){

        $regionId = 'cn-shanghai';
        $accessKeyId = env('aliyun.ali_access_key_id');
        $accessKeySecret = env('aliyun.ali_access_key_secret');
        $client = AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
            ->regionId($regionId)
            ->connectTimeout(1)
            ->timeout(3)
            ->asDefaultClient();


        $data = [
            'title'=>'ceshi',
            'fileName'=>'视频名称.mp4',

        ];
        $request = Vod::v20170321()->createUploadVideo();
        $request->withTitle($data['title']);//标题，UTF8,128大小
        $request->withFileName($data['fileName']);//视频源文件名
//        $request->withDescription(isset($data['description']) ? $data['description'] : '');//描述,utf-8
//        $request->withCoverURL(isset($data['coverURL']) ? $data['coverURL'] : '');//封面url
//        //$request->withCateId(isset($data['cateId']) ? $data['cateId'] : ''); //分类id
//        $request->withTags(isset($data['tags']) ? $data['tags'] : '');      //标签，隔开
//        $request->withTemplateGroupId(isset($data['templateGroupId']) ? $data['templateGroupId'] : ''); //转码模板组ID
//        $request->withStorageLocation(isset($data['storageLocation']) ? $data['storageLocation'] : ''); //存储地址
//        if($isCallback == 1)
//        {
//            $userData = array(
//                "MessageCallback" => array("CallbackURL" => $onDemand->callback),
//                "Extend" => array("title" => $data['title']),
//                'EventType' => 'TranscodeComplete',
//            );
//            $request->withUserData(json_encode($userData));
//        }
        $result = $request->request();

        $videoId = $result->VideoId;
        var_dump($result);
//
//        $uploadAddress = json_decode(base64_decode($result->UploadAddress), true);
//        $uploadAuth = json_decode(base64_decode($result->UploadAuth), true);
//
//        var_dump($uploadAddress);
//        var_dump($uploadAuth);
//        var_dump($videoId);
    }
    /**
     *
     */
    public function tree(){
        try {
            $accessKeyId = env('aliyun.ali_access_key_id');;
            $accessKeySecret = env('aliyun.ali_access_key_secret');
            $filePath = "/www/wwwroot/yike/public/123.mp4";
            $callbackUrl = 'http://api.doctorsclass.com/wxapp/ceshi/callback';
            $uploader = new \AliyunVodUploader($accessKeyId, $accessKeySecret);
            $uploadVideoRequest = new \UploadVideoRequest($filePath, 'testUploadLocalVideo via PHP-SDK');
            //$uploadVideoRequest->setCateId(1);
            //$uploadVideoRequest->setCoverURL("http://xxxx.jpg");
            //$uploadVideoRequest->setTags('test1,test2');
            //$uploadVideoRequest->setStorageLocation('outin-xx.oss-cn-beijing.aliyuncs.com');
            //$uploadVideoRequest->setTemplateGroupId('6ae347b0140181ad371d197ebe289326');
            $userData = array(
                "MessageCallback"=>array("CallbackURL"=>$callbackUrl),
                "Extend"=>array("localId"=>"xxx", "test"=>"www")
            );
            $uploadVideoRequest->setUserData(json_encode($userData));
            $res = $uploader->uploadLocalVideo($uploadVideoRequest);
            print_r($res);
        } catch (\Exception $e) {
            printf("testUploadLocalVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
                $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
        }

    }

}
